package org.example.domain.strategy.service.rule.tree.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import org.example.domain.strategy.service.rule.tree.ILogicTreeNode;
import org.example.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 1- 奖品次数锁节点（实现类）
 * <p>
 * 达到解锁次数就走库存节点 否则走兜底节点
 */

@Slf4j
@Component("rule_lock")
public class RuleLockLogicTreeNode implements ILogicTreeNode {


    private Long userRaffleCount = 0L;


    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue) {


        log.info("规则树过滤--次数锁");
        Long raffleConut = 0L;
        try {
            raffleConut = Long.valueOf(ruleValue);
        } catch (NumberFormatException e) {
            throw new RuntimeException("规则过滤异常 --- 未配置ruleValue值");
        }

        if (userRaffleCount > raffleConut) {
            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckType(RuleLogicCheckTypeVO.ALLOW)
                    .build();
        }

        //抽奖次数小于解锁条件 拦截
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .build();
    }
}
